package com.rocsea.robot.template.impl;

import com.rocsea.robot.config.build.KettleBuildTemplateConfig;
import com.rocsea.robot.config.build.TemplateInfo;
import com.rocsea.robot.factory.KettleTemplateFactory;
import com.rocsea.robot.model.entity.StepAttributeDO;
import com.rocsea.robot.model.enums.KettleBuildStrategyEnum;
import com.rocsea.robot.service.KettlePackService;
import com.rocsea.robot.template.AbstractTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 分表按年拆分-轻量级
 * @Author RocSea
 * @Date 2022/12/7
 */
@Component
public class ShardingAnnuallyLightTemplate extends AbstractTemplate {
    @Resource
    private KettleBuildTemplateConfig config;

    @Resource
    private KettlePackService kettlePackService;

    @Override
    public TemplateInfo getTemplateInfo() {
        return config.getShardingLight();
    }


    @Override
    public String processTableInput(List<StepAttributeDO> stepAttributeDOS) {
        return super.processTableInputNoRowMeta(stepAttributeDOS);
    }

    @Override
    public String processSqlReplace(String sql) {
        return sql.substring(sql.indexOf("FROM"));
    }

    @Override
    public String processQueryFromTableName(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processKettlePack() {
        return kettlePackService.buildKettlePackWithSharding();
    }

    @Override
    public String processKettleTaskConfig() {
        return "";
    }

    @Override
    public void afterPropertiesSet() {
        KettleTemplateFactory.register(KettleBuildStrategyEnum.SHARDING_ANNUALLY_LIGHT.getText(), this);
    }

}
